﻿using System;
using System.Xml;

namespace FrontEnd.Storage
{
    ///-------------------------------------------------------------------------------------------------
    /// <summary> Represents a single game in a game list. </summary>
    ///-------------------------------------------------------------------------------------------------
    public class Game
    {
        #region Xml Constants

        private const String CUSTOM_DESCRIPTION_ATTR = "Description";
        private const String CLONE_OF_ATTR = "CloneOf";

        #endregion

        #region Properties

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Gets or sets the name of the game, used to identify the file/folder where roms are
        /// stored. </summary>
        /// <value> The name. </value>
        ///-------------------------------------------------------------------------------------------------
        public String Name { get; set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Gets or sets the parent (if applicable.) </summary>
        /// <value> The parent (if applicable.) </value>
        ///-------------------------------------------------------------------------------------------------
        public String CloneOf { get; set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Gets or sets custom description assigned by the user. </summary>
        /// <value> Custom description. </value>
        ///-------------------------------------------------------------------------------------------------
        public String CustomDescription { get; set; }

        #endregion

        #region Constructor(s)

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Default constructor. </summary>
        ///-------------------------------------------------------------------------------------------------
        public Game()
        {
            Clear();
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Copy constructor. </summary>
        /// <param name="game"> The game. </param>
        ///-------------------------------------------------------------------------------------------------
        public Game(Game game)
        {
            Copy(game);
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. </summary>
        /// <param name="node"> The node to load from. </param>
        ///-------------------------------------------------------------------------------------------------
        public Game(XmlNode node)
        {
            LoadFrom(node);
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. </summary>
        /// <param name="name">              The name. </param>
        /// <param name="cloneOf">           The parent (if applicable.) </param>
        /// <param name="customDescription"> Custom description. </param>
        ///-------------------------------------------------------------------------------------------------
        public Game(String name, String cloneOf, String customDescription)
        {
            Name = name;
            CloneOf = cloneOf;
            CustomDescription = customDescription;
        }

        #endregion

        #region Public

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Clears this object to its blank/initial state. </summary>
        ///-------------------------------------------------------------------------------------------------
        public void Clear()
        {
            Name = null;
            CloneOf = null;
            CustomDescription = null;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Copies the given game. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
        /// <param name="game"> The game. </param>
        ///-------------------------------------------------------------------------------------------------
        public void Copy(Game game)
        {
            if (game == null)
                throw new ArgumentNullException("game");

            Name = game.Name;
            CloneOf = game.CloneOf;
            CustomDescription = game.CustomDescription;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Loads from the specified node. NOTE: No argument checking to increase performance. </summary>
        /// <param name="node"> The node to load from. </param>
        ///-------------------------------------------------------------------------------------------------
        public void LoadFrom(XmlNode node)
        {
            Name = node.InnerText;

            if (node.Attributes[CLONE_OF_ATTR] == null)
                CloneOf = null;
            else
                CloneOf = node.Attributes[CLONE_OF_ATTR].Value;

            if (node.Attributes[CUSTOM_DESCRIPTION_ATTR] == null)
                CustomDescription = null;
            else
                CustomDescription = node.Attributes[CUSTOM_DESCRIPTION_ATTR].Value;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Saves to the specified node. NOTE: No argument checking to increase performance. </summary>
        /// <param name="node"> The node to save to. </param>
        ///-------------------------------------------------------------------------------------------------
        public void SaveTo(XmlNode node)
        {
            node.InnerText = (Name == null) ? "" : Name;

            if (!String.IsNullOrWhiteSpace(CloneOf))
            {
                XmlAttribute attr = node.OwnerDocument.CreateAttribute(CLONE_OF_ATTR);
                attr.Value = CloneOf;
                node.Attributes.Append(attr);
            }
            
            if (!String.IsNullOrWhiteSpace(CustomDescription))
            {
                XmlAttribute attr = node.OwnerDocument.CreateAttribute(CUSTOM_DESCRIPTION_ATTR);
                attr.Value = CustomDescription;
                node.Attributes.Append(attr);
            }            
        }

        #endregion
    }
}
